﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace ebMS3dotnet.Core.Configuration
{
    class SoapVersionConverter:ConfigurationConverterBase
    {
        public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext ctx, Type type)
        {
            return type == typeof(EnvelopeVersion);
        }

        public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext ctx, Type type)
        {
            return type == typeof(string);
        }

        public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string soapVersion = (string)value;

            switch (soapVersion)
            {
                case "Soap11":
                    return EnvelopeVersion.Soap11;

                case "Soap12":
                    return EnvelopeVersion.Soap12;
            }

            throw new ArgumentException("Unknown Soap Version");
        }

        public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            EnvelopeVersion version = (EnvelopeVersion)value;
            if (version == EnvelopeVersion.Soap11)
                return "Soap11";

            if (version == EnvelopeVersion.Soap12)
                return "Soap12";

            throw new ArgumentException("Only Soap11 or Soap12 are valid.");            
        }
    }
}
